<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\ding;

use app\common\model\ding\Friend as FriendModel;
use think\facade\Db;

/**
 * 商品评价模型
 * Class Article
 * @package app\api\model
 */
class Friend extends FriendModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'is_delete',
        'store_id',
        'create_time',
        'update_time',
    ];

    public function addView($clerkId, $friendId)
    {
        $data['clerk_id']    = $clerkId;
        $data['friend_id']   = $friendId;
        $data['store_id']    = self::$storeId;
        $data['create_time'] = time();
        $data['date']        = Date('Y-m-d');
        Db::name('ding_friend_copy_log')->insert($data);
        return true;
    }

    public function edit($data)
    {

        $this->save($data);
        return true;
    }
    public static function getDetail(int $friendId)
    {
        // 获取文章详情
        $detail = parent::detail($friendId, ['imagePoster']);
        if (empty($detail) || $detail['is_delete']) {
            throwError('很抱歉，当前文案不存在');
        }

        return $detail;
    }
    /**
     * 获取文章列表
     * @param int $categoryId
     * @param int $limit
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList($param, int $limit = 200)
    {
        $params = $this->setQueryDefaultValue($param, [

            'keyword'    => '',
            'type'       => -1,
            'categoryId' => -1,
        ]);
        // 检索查询条件
        $filter = [];
        if ($params['categoryId'] == 0) {
            $params['categoryId'] = Db::name('ding_friend_category')
                ->where('status', 1)->order('sort asc')->value('category_id');
        }
        $params['categoryId'] > 0 && $filter[] = ['category_id', '=', $params['categoryId']];
        // 获取列表数据
        return $this->with(['image', 'imagePoster'])

            ->where($filter)

            ->where('is_delete', '=', 0)
            ->order(['sort' => 'asc'])
            ->paginate($limit);
    }

}
